//
//  CardCollectionViewLayout.m
//  Base_App
//
//  Created by Yutian Duan on 2019/5/28.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "CardCollectionViewLayout.h"

@implementation CardCollectionViewLayout

- (instancetype)initWithItemSize:(CGSize)itemSize {
  if (self = [super init]) {
    //设置item的大小
    self.itemSize = itemSize;
    self.scrollDirection  = UICollectionViewScrollDirectionHorizontal;
    self.sectionInset = UIEdgeInsetsMake(0, ScreenWidth/2-itemSize.width/2, 0, ScreenWidth/2-itemSize.width/2);
    
  }
  return self;
}

- (instancetype)init {
  
  if (self = [super init]) {
    CGFloat width = ScreenWidth * 0.45;
    //设置item的大小
    CGSize size = CGSizeMake(width, width * 1.25f + 50);
    
    self.itemSize = size;
    self.scrollDirection  = UICollectionViewScrollDirectionHorizontal;
    self.sectionInset = UIEdgeInsetsMake(0, ScreenWidth/2-size.width/2, 0, ScreenWidth/2-size.width/2);
    self.minimumLineSpacing = 20;
  }
  return self;
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  NSArray *arr = [self copyAttributes: [super layoutAttributesForElementsInRect:rect]];
  //屏幕中间线
  CGFloat centerX = self.collectionView.contentOffset.x  + self.collectionView.bounds.size.width /2.0f;
  //刷新cell缩放
  for (UICollectionViewLayoutAttributes *attribute in arr) {
    CGFloat distance = fabs(attribute.center.x - centerX);
    //移动的距离和屏幕宽的比例
    CGFloat screenScale = distance /self.collectionView.bounds.size.width;
    //卡片移动到固定范围内 -π/4 到 π/4
    CGFloat scale = fabs(cos(screenScale * M_PI/4));
    //设置cell的缩放 按照余弦函数曲线  越居中越接近于1
    attribute.transform = CGAffineTransformMakeScale(1.0, scale);
    //透明度
    attribute.alpha = scale;
  }
  
  
  return arr;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
  return YES;
}

- (NSArray *)copyAttributes:(NSArray  *)arr {
  NSMutableArray *copyArr = [NSMutableArray new];
  for (UICollectionViewLayoutAttributes *attribute in arr) {
    [copyArr addObject:[attribute copy]];
  }
  
  
  return copyArr;
}

@end
